package com.zhentao.controller;

import com.zhentao.login.service.TUserService;
import com.zhentao.login.utils.AuthContextUtil;
import com.zhentao.login.utils.JwtToken;
import com.zhentao.login.utils.Result;
import com.zhentao.nlz.project.login.TUser;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

@RestController
@RequestMapping("user")
public class TUserController {
    @Autowired
    private JwtToken jwtToken;

    @Autowired
    private TUserService userService;

    @Autowired
    private TUserService tUserService;
    @GetMapping("getUserinfo")
    public Result getUserinfo() {
        System.out.println("执行getUserinfo方法");
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String token = request.getHeader("token");
        String substring = token.substring(1, token.length() - 1);
        //根据token获取用户id
        TUser user = (TUser) jwtToken.getLoginUser(substring);
        System.out.println(user);
        System.out.println(token+"token111");
        if (user == null) {
            return Result.error("用户未登录");
        }
        return Result.success(user);
    }
    @GetMapping("getUsers")
    public TUser getUsers() {
        TUser user = tUserService.getUsers();
        return user;
    }
}
